var inervalId;
var UUID;

// 报错信息写入后台日志表log_error
function writeErrLog(func, remark){
    const count = arguments.length;
    // 根据参数个数构建请求用的参数
    // 2个参数：func,remark
    // 3个参数：func,remark,errMsg
    if(count <= 0) return;
    let data = {};
    if(count === 2) data = {module:'web管理页面Scancodetologin.js',func:arguments[0],remark:arguments[1]};
    if(count === 3) data = {module:'web管理页面Scancodetologin.js',func:arguments[0],remark:arguments[1],errMsg:arguments[2]};

    $.ajax({
        type: 'POST', dataType: 'json', contentType: "application/json;charset=UTF-8",
        url :'https://showawx.xdfznh.club/visitorbe/logError/insert',
        data:JSON.stringify(data),
        success : function(res) {
            const { success, msg } = res;
            if(!success) $.messager.alert('提示', msg, 'warning');
        },
        error : function(err){
            console.log('写入后台错误日志表时出现异常：', err);
        }
    })
}

$(function () {
    setDJS();

    // setInterval(function() {
    //
    // }, 1000);
});

//获取二维码
function getQRcode() {
    UUID=guid();
    $.ajax({
        url :'https://showawx.xdfznh.club/visitorbe/wxmp/qrCodeUrl',
        type: 'POST',
        data:JSON.stringify({"permanent":0,"type":3,"sceneData":UUID}),
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        success : function(cdata) {
            let data=cdata.data;
            document.getElementById('ewm').src=data;
        },
        //请求失败，包含具体的错误信息
        error : function(e){
            console.log('请求获取二维码失败');
            document.getElementById('ewm').src="img/320.png";
            clearInterval(inervalId);
            document.getElementById('lab3').innerHTML="获取登录二维码失败！";
        }
    })
}

function clearLogMsg(uuid){
    $.ajax({
        url: 'https://showawx.xdfznh.club/visitorbe/loginQrLog/clearMsg/' + UUID,
        type: 'post',
        contentType: "application/json;charset=UTF-8",
        success: function (res) {
            const { success, msg } = res;
            if(!success) $.messager.alert('提示', msg, 'warning');
        }
    })
}

//判断是否扫码  扫码之后直接登录
function islogin() {
    $.ajax({
        url:'https://showawx.xdfznh.club/visitorbe/loginQrLog/isLogged/'+UUID,
        type: 'post',
        contentType: "application/json;charset=UTF-8",
        success: function (res) {
            const { success,data,msg } = res;
            if(!success){
                $.messager.alert('提示', msg, 'warning');
                // 清除 login_qr_log.msg
                clearLogMsg(UUID);
                return
            }
            if(data){
                let str=data.openId +','+ data.unionId+','+ data.realName;
                window.sessionStorage.setItem("data",str);
                window.location.replace("whiteMain.html");
            }else{
                //writeErrLog('islogin','登录成功但是没有响应来用户信息');
            }
        },
        error: function(err){
            writeErrLog('islogin','登录时出现异常，见字段err_msg', JSON.stringify(err));
        }
    })
}



function setDJS() {
    var count=0;
    var num=120;
    // document.getElementById('ewm').src="img/320.png";
    getQRcode();  //获取二维码 然后显示到登录页面
    inervalId = setInterval(function() {
        count++;
        document.getElementById('lab3').innerHTML=num+" 秒后二维码失效";
        islogin();  //判断是否扫码 可以登录
        num--;
        if (count==120){
            clearInterval(inervalId);
            document.getElementById('lab3').innerHTML="二维码失效";
            document.getElementById('ewm').src="img/320.png";
        }
    }, 1000);
}

function shuaxin() {
    clearInterval(inervalId);
    setDJS()
}

//生成UUID
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}


function getusename(openid) {
    $.ajax({
        url:'https://www.barrel.fit/barrelbe/user/selectByOpenId/'+openid,
        type: 'post',
        // dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        success: function (data) {
            let userName=data.data.realName;
            let str=openid +','+ userName;
            window.sessionStorage.setItem("data",str);
            window.location.replace("whiteMain.html");
        },
        //失败回调
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //查看错误信息
            console.log('失败回调');
        }

    })
}